<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never" v-loading="loading">
        <!-- 搜索栏 -->
        <el-form
          :inline="true"
          :model="pagination"
          ref="pagination"
        >
          <el-form-item label="商家编号：" prop="storeId">
            <el-input v-model="pagination.code" placeholder="请输入商家编号"></el-input>
          </el-form-item>
          <el-form-item label="商家名称：" prop="money">
            <el-input v-model="pagination.simpleName" placeholder="请输入商家名称"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="handleRest">重置</el-button>
          </el-form-item>
        </el-form>
        <!-- 搜索栏 / -->
        <!--        <div class="btnBox" style="text-align:right">-->
        <!--          <el-button type="primary" @click="doQuery">待支付</el-button>-->
        <!--          <el-button type="primary" @click="doQuery">已支付</el-button>-->
        <!--        </div>-->
        <!-- 数据表格 -->
        <el-table
          :data="items"
          border
          style="width: 100%; margin-top:10px;"
          @selection-change="handleSelectionChange"
        >
          <el-table-column prop="id" type="index" label="#"></el-table-column>
          <el-table-column prop="code" label="商家编号"></el-table-column>
          <el-table-column prop="simpleName" label="商家名称"></el-table-column>
          <el-table-column prop="billMoney" label="账单金额"></el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button @click="() => $router.push({path: 'accounts-payList', query: {id: scope.row.id}})" type="text" size="small">支付</el-button>
              <!--              <el-button @click="handleEdit(scope.row)" type="text" size="small">修改</el-button>-->
              <!--              <el-button @click="handleDelect(scope.row)" type="text" size="small">删除</el-button>-->
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="Number(total)"
        ></el-pagination>
        <!-- 数据表格 / -->
      </el-card>
      <!-- 新增标签弹层 -->
      <Dialog ref="addType" :dialogTitle="dialogTitle" v-on:newDataes = subMitForm></Dialog>
    </div>
  </div>
</template>

<script>
  import { getDetails, delData } from '@/api/accounts'
  import { getList } from '@/api/store'
  import Dialog from './../components/accountsAdd'
  export default {
    name: 'accountsList',
    components: {
      Dialog
    },
    data() {
      return {
        items: [
          {
          }
        ],
        total: 20,
        pagination: {
          page: 1,
          pageSize: 10,
          code: '',
          simpleName: '',
          type: '1'
        },
        loading: false,
        multipleSelection: [],
        dialogVisible: false,
        dialogTitle: ''
      }
    },
    created() {
      this.doQuery(this.pagination)
    },
    methods: {
      // 业务方法
      async doQuery(parent) {
        this.loading = true
        // this.items = []
        await getList(parent)
          .then(res => {
            const { data } = res
            this.items = data.items
            this.total = data.counts
            this.loading = false
          })
          .catch(err => {
            console.log(err)
            this.loading = false
          })
      },
      // UI方法 - 重置
      handleRest() {
        this.pagination.code = ''
        this.pagination.simpleName = ''
        this.$refs['pagination'].resetFields()
      },
      // 搜索
      handleSearch() {
        this.doQuery(this.pagination)
      },
      //
      handleSelectionChange(val) {
        console.log(val)
        this.multipleSelection = val
      },
      // 每页条数
      handleSizeChange(val) {
        // console.log(`每页 ${val} 条`)
        this.pagination.pageSize = val
        this.doQuery(this.pagination)
      },
      // 当前页
      handleCurrentChange(val) {
        // console.log(`当前页: ${val}`)
        this.pagination.page = val
        this.doQuery(this.pagination)
      },
      // 添加
      handleAdd(val) {
        this.dialogTitle = '添加'
        this.$refs.addType.dialogFormV()
      },
      // 详情获取  - 如果有详情接口 读详情
      async getDetails(id) {
        let dataes = {}
        await getDetails(id)
          .then(res => {
            const { data } = res
            this.loading = false
            dataes = data
          })
          .catch(err => {
            console.log(err)
            this.loading = false
          })
        return dataes
      },
      // 编辑
      async handleEdit (val) {
        this.dialogTitle = '编辑'
        // 如果有详情接口 读详情
        const data = await this.getDetails(val.id)
        // 如果列表中的数据可以满足修改的信息展示  则不用读取上面的详情接口 用下面的data
        // const data = val
        this.$refs.addType.dialogFormV(data)
      },
      // 删除
      handleDelect(item) {
        this.$confirm('是否确认删除？')
          .then(async ret => {
            await delData(item.id)
              .then(res => {
                console.log(res)
                this.loading = false
                this.$message({
                  message: '删除成功',
                  type: 'success'
                })
                this.doQuery(this.pagination)
              })
              .catch(err => {
                console.log(err)
                this.loading = false
              })
          })
          .catch(ret => {
            console.log(ret)
          })
      },
      // subMitForm
      subMitForm(val) {
        if (val.id) {
          this.$message.success('修改成功')
        } else {
          this.$message.success('添加成功')
        }
        this.doQuery(this.pagination)
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .alert {
    margin: 10px 0px 0px 0px;
  }
  .pagination {
    margin-top: 10px;
    text-align: right;
  }
</style>
